/**
 * 
 */
package br.ufal.ic.sd.project.server.helper;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;

import br.ufal.ic.sd.project.server.exception.ServerException;
import br.ufal.ic.sd.project.server.util.Constants;

import com.sun.jmx.snmp.Timestamp;

/**
 * @author keven andrade
 *
 */
public class Helper {
    
    public static boolean isProject(String strRepository, String strProjectName){
	
	File directory = new File(strRepository);

	if(!directory.isDirectory()){

	    if(!directory.mkdir()){
		System.err.println("Error creating repository!!");
		return false;
	    }
	    
	}
	    
	for(File file : directory.listFiles()){
	    
	    if(file.getName().toUpperCase().split(".LOGBYK")[0].equals(strProjectName.toUpperCase())){
		return true; 
	    }
	}
	
	return false;
    }

    public static boolean createNewProject(String strRepository, String strProjectName) {
	
	File directory = new File(strRepository);

	if(!directory.isDirectory()){
	    
	   if(!directory.mkdir()){
	       System.err.println("Error creating repository!!");
	       return false;
	   }
	    
	}
	
	File log = new File(strRepository + strProjectName  + ".logbyk.log");
	    
	FileWriter writer;
	PrintWriter pwAdd;
		
	try {
			
	    //add the file on the list of all files
			
	    writer = new FileWriter(log, true);
			
	    pwAdd = new PrintWriter(writer);
			
	    pwAdd.println("This file contains the log of application " + strProjectName + " generated since " + new Timestamp(System.currentTimeMillis()).getDate() +"\nusing the LogByK log API.");
	    pwAdd.println("");
	    pwAdd.println("");

	    pwAdd.close();  
	    writer.close();

	} catch (IOException e) {
	    System.err.println(e.getMessage());
	    return false;
	}
	
	return true;
    }

    public static String addLog(String strRepository, String strProject, String strTimeLogOcurrence, String strUser,
	    String strClass, String strLogType, String strLogMessage) {


	FileWriter writer;
	PrintWriter pwAdd;
	
	try {
		
		//add the file on the list of all files
		
		writer = new FileWriter(strRepository + strProject + ".logbyk.log", true);
		
		pwAdd = new PrintWriter(writer);
		
		pwAdd.println(strTimeLogOcurrence + " [" + strUser + "] " + strClass + " [" + strLogType + "] " + strLogMessage);  
		 
		pwAdd.close();  
		writer.close();
		
		
	} catch (IOException e) {
		System.err.println(e.getMessage());
	}
	
	return strProject + ".logbyk.log";
	
    }
    
    public static Properties loadFile(String strFileName) throws ServerException{
	
	Properties objProperties = new Properties();
	
	try {
	    objProperties.load(new FileInputStream(strFileName));
	} catch (FileNotFoundException e) {
	    throw new ServerException("File not Found");
	} catch (IOException e) {
	    throw new ServerException("File not Found");
	}
	
	return objProperties;
    }
    
    public static void createFile(String strRepository, String strFileName, String strFileContent) {

	FileWriter writer;
	PrintWriter pwAdd;
	
	File file = new File(strRepository + strFileName);
	
	try {
		
		//add the file on the list of all files
		
		writer = new FileWriter(file);
		
		pwAdd = new PrintWriter(writer);
		
		pwAdd.println(strFileContent);  
		 
		pwAdd.close();  
		writer.close();
		
		
	} catch (IOException e) {
		System.err.println(e.getMessage());
	}
    }
    
    public static String getServerRepository(){
	
	Properties objProperties = null;
	
	try {
	    objProperties = loadFile(Constants.SERVER_PROPERTIES);
	    
	} catch (ServerException e) {
	    e.printStackTrace();
	}
	
	return objProperties.getProperty(Constants.REPOSITORY);
    }

}
